<template>
  <router-view
    :style="{ background: this.$store.state.pageBackground }"
    style="height:100%;"
  />
</template>

<script>
export default {
  mounted() {
    this.setHeader();
  },
  methods: {
    //将租户Code放入请求头
    setHeader() {
      // this.saveUserInfo();
      if (this.$keycloak.authenticated) {
        // rt.setRequestHeaders('Authorization', 'Bearer ' + this.$keycloak.token);
        rt.setRequestInterceptors(config => {
          config.headers.Authorization = `Bearer ${this.$keycloak.token}`;
          this.updateAuth();
          return config;
        });
        this.saveUserInfo();
      }
    },
    saveUserInfo() {
      let user = {
        userId: null,
        userName: this.$keycloak.userName,
        nickName: null
      };
      this.$store.commit('UPDATE_USER', user);
      this.updateAuth();
      this.$router.push({ path: '/' });
    },
    updateAuth() {
      const auth = this.$store.state.auth;
      auth.isLoggedIn = true;
      auth.accessToken = this.$keycloak.token;
      auth.refreshToken = this.$keycloak.refreshToken;
      this.$store.commit('UPDATE_AUTH', auth);
    }
  }
};
</script>

<style lang="sass">
@import '~vue-xpc/src/style/index.scss';
@import '../element-variables.scss';
@import './style/index.scss';
</style>
